﻿Imports System
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.Collections
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Globalization
Imports System.IO
Imports System.Reflection
Imports System.Resources
Imports System.Runtime.InteropServices
Imports System.Threading
Imports System.Web
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler

Namespace System.Workflow.ComponentModel.Design
    Friend Class DesignerHelpers
        ' Methods
        Friend Shared Function AreAssociatedDesignersMovable(ByVal components As ICollection) As Boolean
            Dim obj2 As Object
            For Each obj2 In components
                Dim activity As Activity = [TryCast](obj2,Activity)
                If (activity Is Nothing) Then
                    Dim info As HitTestInfo = [TryCast](obj2,HitTestInfo)
                    activity = If(((Not info Is Nothing) AndAlso (Not info.AssociatedDesigner Is Nothing)), info.AssociatedDesigner.Activity, Nothing)
                End If
                Dim designer As ActivityDesigner = ActivityDesigner.GetDesigner(activity)
                If ((Not designer Is Nothing) AndAlso designer.IsLocked) Then
                    Return False
                End If
            Next
            Return True
        End Function

        Friend Shared Function AreComponentsRemovable(ByVal components As ICollection) As Boolean
            If (components Is Nothing) Then
                Throw New ArgumentNullException("components")
            End If
            Dim obj2 As Object
            For Each obj2 In components
                Dim activity As Activity = [TryCast](obj2,Activity)
                Dim info As ConnectorHitTestInfo = [TryCast](obj2,ConnectorHitTestInfo)
                If ((activity Is Nothing) AndAlso (info Is Nothing)) Then
                    Return False
                End If
                If (Not activity Is Nothing) Then
                    Dim designer As ActivityDesigner = ActivityDesigner.GetDesigner(activity)
                    If ((Not designer Is Nothing) AndAlso designer.IsLocked) Then
                        Return False
                    End If
                End If
                If ((Not info Is Nothing) AndAlso Not TypeOf info.AssociatedDesigner Is FreeformActivityDesigner) Then
                    Return False
                End If
            Next
            Return True
        End Function

        Friend Shared Function CreateUniqueMethodName(ByVal component As IComponent, ByVal propName As String, ByVal delegateType As Type) As String
            Dim serviceProvider As IServiceProvider = component.Site
            If (serviceProvider Is Nothing) Then
                Throw New ArgumentException("component")
            End If
            Dim host As IDesignerHost = [TryCast](serviceProvider.GetService(GetType(IDesignerHost)),IDesignerHost)
            If (host Is Nothing) Then
                Throw New InvalidOperationException(SR.GetString("General_MissingService", New Object() { GetType(IDesignerHost).FullName }))
            End If
            If (DirectCast(serviceProvider.GetService(GetType(ITypeProvider)), ITypeProvider) Is Nothing) Then
                Throw New InvalidOperationException(SR.GetString("General_MissingService", New Object() { GetType(ITypeProvider).FullName }))
            End If
            Dim name As String = Nothing
            Dim service As IReferenceService = [TryCast](serviceProvider.GetService(GetType(IReferenceService)),IReferenceService)
            If (Not service Is Nothing) Then
                name = service.GetName(component)
            Else
                Dim site As ISite = component.Site
                If (Not site Is Nothing) Then
                    name = site.Name
                End If
            End If
            If (name Is Nothing) Then
                name = component.GetType.Name
            End If
            name = (name.Replace("."c, "_"c).Replace("/"c, "_"c) & "_" & propName).Replace("("c, "_"c).Replace(")"c, "_"c).Replace(" ", "")
            Dim info As New DelegateTypeInfo(delegateType)
            Dim rootComponent As Activity = [TryCast](host.RootComponent,Activity)
            If (rootComponent Is Nothing) Then
                Dim activity2 As Activity = [TryCast](component,Activity)
                Throw New InvalidOperationException(SR.GetString("Error_CantCreateMethod", New Object() { If((Not activity2 Is Nothing), activity2.QualifiedName, String.Empty) }))
            End If
            Dim dataSourceClass As Type = Helpers.GetDataSourceClass(rootComponent, serviceProvider)
            If (dataSourceClass Is Nothing) Then
                Throw New InvalidOperationException(SR.GetString("Error_CantCreateMethod", New Object() { rootComponent.QualifiedName }))
            End If
            Dim bindingAttr As BindingFlags = (BindingFlags.FlattenHierarchy Or (BindingFlags.NonPublic Or (BindingFlags.Public Or (BindingFlags.Static Or BindingFlags.Instance))))
            Dim methods As MethodInfo() = dataSourceClass.GetMethods(bindingAttr)
            Dim list As New ArrayList
            Dim info2 As MethodInfo
            For Each info2 In methods
                If (info2.GetParameters.Length = info.Parameters.Length) Then
                    Dim flag As Boolean = True
                    Dim i As Integer
                    For i = 0 To info.Parameters.Length - 1
                        Dim info3 As ParameterInfo = info2.GetParameters(i)
                        Dim expression As CodeParameterDeclarationExpression = info.Parameters(i)
                        Dim direction As FieldDirection = expression.Direction
                        If ((((direction = FieldDirection.In) AndAlso Not info3.IsIn) OrElse ((direction = FieldDirection.Out) AndAlso Not info3.IsOut)) OrElse (((direction = FieldDirection.Ref) AndAlso (Not info3.IsIn OrElse Not info3.IsOut)) OrElse Not Helpers.TypesEqual(expression.Type, info3.ParameterType))) Then
                            flag = False
                            Exit For
                        End If
                    Next i
                    If flag Then
                        list.Add(info2.Name)
                    End If
                End If
            Next
            Dim num2 As Integer = 0
            Dim flag2 As Boolean = True
            Dim strB As String = name
            Dim members As MemberInfo() = dataSourceClass.GetMembers
            Do While (flag2 AndAlso (num2 < &H7FFFFFFF))
                flag2 = False
                Dim str3 As String
                For Each str3 In list
                    If (String.Compare(str3, strB, StringComparison.OrdinalIgnoreCase) = 0) Then
                        flag2 = True
                        Exit For
                    End If
                Next
                If Not flag2 Then
                    Dim info4 As MemberInfo
                    For Each info4 In members
                        If (Not TypeOf info4 Is MethodInfo AndAlso (String.Compare(info4.Name, strB, StringComparison.OrdinalIgnoreCase) = 0)) Then
                            flag2 = True
                            Exit For
                        End If
                    Next
                End If
                If Not flag2 Then
                    Dim info5 As MethodInfo = host.RootComponent.GetType.GetMethod(strB, bindingAttr, Nothing, info.ParameterTypes, Nothing)
                    If ((Not info5 Is Nothing) AndAlso Not info5.IsPrivate) Then
                        flag2 = True
                    End If
                End If
                If flag2 Then
                    strB = (name & "_" & ++num2.ToString(CultureInfo.InvariantCulture))
                End If
            Loop
            Return strB
        End Function

        Friend Shared Function DeserializeDesignerStates(ByVal designerHost As IDesignerHost, ByVal reader As BinaryReader) As Boolean
            Dim num As Integer = reader.ReadInt32
            Dim flag As Boolean = (num <> designerHost.Container.Components.Count)
            Dim i As Integer
            For i = 0 To num - 1
                Dim str As String = reader.ReadString
                Dim num3 As Integer = reader.ReadInt32
                If (Not designerHost.Container.Components.Item(str) Is Nothing) Then
                    Dim designer As ActivityDesigner = [TryCast](designerHost.GetDesigner(designerHost.Container.Components.Item(str)),ActivityDesigner)
                    If (Not designer Is Nothing) Then
                        DirectCast(designer, IPersistUIState).LoadViewState(reader)
                    Else
                        flag = True
                        Dim baseStream As Stream = reader.BaseStream
                        baseStream.Position = (baseStream.Position + num3)
                    End If
                Else
                    flag = True
                    Dim stream2 As Stream = reader.BaseStream
                    stream2.Position = (stream2.Position + num3)
                End If
            Next i
            Return flag
        End Function

        Friend Shared Function GenerateUniqueIdentifier(ByVal serviceProvider As IServiceProvider, ByVal baseIdentifier As String, ByVal existingNames As String()) As String
            Dim provider As CodeDomProvider = Nothing
            If (Not serviceProvider Is Nothing) Then
                provider = [TryCast](serviceProvider.GetService(GetType(CodeDomProvider)),CodeDomProvider)
                If (provider Is Nothing) Then
                    Dim service As IdentifierCreationService = [TryCast](serviceProvider.GetService(GetType(IIdentifierCreationService)),IdentifierCreationService)
                    If (Not service Is Nothing) Then
                        provider = service.Provider
                    End If
                End If
            End If
            If (Not provider Is Nothing) Then
                baseIdentifier = provider.CreateValidIdentifier(baseIdentifier)
            End If
            baseIdentifier = baseIdentifier.Replace("."c, "_"c)
            baseIdentifier = baseIdentifier.Replace("/"c, "_"c)
            baseIdentifier = baseIdentifier.Replace("("c, "_"c)
            baseIdentifier = baseIdentifier.Replace(")"c, "_"c)
            baseIdentifier = baseIdentifier.Replace(" ", "")
            Dim list As New ArrayList(existingNames)
            Dim num As Integer = 1
            Dim str As String = String.Format(CultureInfo.InvariantCulture, "{0}{1}", New Object() { baseIdentifier, num })
            list.Sort
            Do While (list.BinarySearch(str.ToLowerInvariant, StringComparer.OrdinalIgnoreCase) >= 0)
                str = String.Format(CultureInfo.InvariantCulture, "{0}{1}", New Object() { baseIdentifier, num })
                num += 1
            Loop
            Return str
        End Function

        Friend Shared Function GetDeclarativeRules(ByVal activity As Activity) As IDictionary([Of] String, String)
            Dim dictionary As IDictionary([Of] String, String) = New Dictionary([Of] String, String)
            Dim type As Type = Type.GetType("System.Workflow.Activities.Rules.RuleConditionReference, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", False)
            If (Not type Is Nothing) Then
                Dim descriptor As PropertyDescriptor
                For Each descriptor In TypeDescriptor.GetProperties(activity)
                    Dim obj2 As Object = descriptor.GetValue(activity)
                    If ((Not obj2 Is Nothing) AndAlso type.IsAssignableFrom(obj2.GetType)) Then
                        Dim descriptor2 As PropertyDescriptor = TypeDescriptor.GetConverter(obj2).GetProperties(New TypeDescriptorContext(activity.Site, Nothing, obj2), obj2).Item("ConditionName")
                        Dim descriptor3 As PropertyDescriptor = TypeDescriptor.GetConverter(obj2).GetProperties(New TypeDescriptorContext(activity.Site, Nothing, obj2), obj2).Item("Expression")
                        If ((Not descriptor2 Is Nothing) AndAlso (Not descriptor3 Is Nothing)) Then
                            Dim str As String = [TryCast](descriptor2.GetValue(obj2),String)
                            Dim obj3 As Object = descriptor3.GetValue(obj2)
                            If (Not String.IsNullOrEmpty(str) AndAlso Not dictionary.ContainsKey(str)) Then
                                Dim str2 As String = If((Not obj3 Is Nothing), [TryCast](descriptor3.Converter.ConvertTo(New TypeDescriptorContext(activity.Site, Nothing, obj2), Thread.CurrentThread.CurrentUICulture, obj3, GetType(String)),String), Nothing)
                                If (str2 Is Nothing) Then
                                    str2 = String.Empty
                                End If
                                dictionary.Add(str, str2)
                            End If
                        End If
                    End If
                Next
            End If
            Return dictionary
        End Function

        Friend Shared Function GetDesignerActionVerbs(ByVal designer As ActivityDesigner, ByVal designerActions As ReadOnlyCollection([Of] DesignerAction)) As DesignerVerb()
            Dim list As New List([Of] DesignerVerb)
            Dim i As Integer
            For i = 0 To designerActions.Count - 1
                Dim item As New DesignerVerb(designerActions.Item(i).Text, New EventHandler(AddressOf New EventHandler(AddressOf DesignerHelpers.OnExecuteDesignerAction).Invoke), New CommandID(WorkflowMenuCommands.MenuGuid, (WorkflowMenuCommands.VerbGroupDesignerActions + i)))
                item.Properties.Item(DesignerUserDataKeys.DesignerAction) = designerActions.Item(i)
                item.Properties.Item(DesignerUserDataKeys.Designer) = designer
                list.Add(item)
            Next i
            Return list.ToArray
        End Function

        Friend Shared Function GetImageFromPath(ByVal designerTheme As DesignerTheme, ByVal directory As String, ByVal path As String) As Image
            Dim bitmap As Bitmap = Nothing
            If (path.Contains(Path.DirectorySeparatorChar.ToString) AndAlso (directory.Length > 0)) Then
                Dim str As String = HttpUtility.UrlDecode(New Uri(New Uri(directory), path).LocalPath)
                If File.Exists(str) Then
                    Try 
                        bitmap = New Bitmap(str)
                    Catch obj1 As Object
                    End Try
                End If
            ElseIf (Not designerTheme.DesignerType Is Nothing) Then
                Dim length As Integer = path.LastIndexOf("."c)
                If (length > 0) Then
                    Dim baseName As String = path.Substring(0, length)
                    Dim name As String = path.Substring((length + 1))
                    If (((Not baseName Is Nothing) AndAlso (baseName.Length > 0)) AndAlso ((Not name Is Nothing) AndAlso (name.Length > 0))) Then
                        Try 
                            Dim manager As New ResourceManager(baseName, designerTheme.DesignerType.Assembly)
                            bitmap = [TryCast](manager.GetObject(name),Bitmap)
                        Catch obj2 As Object
                        End Try
                    End If
                End If
            End If
            If (Not bitmap Is Nothing) Then
                bitmap.MakeTransparent(AmbientTheme.TransparentColor)
            End If
            Return bitmap
        End Function

        Friend Shared Function GetNextSelectableActivity(ByVal currentActivity As Activity) As Activity
            Dim obj2 As Object
            Dim designer As ActivityDesigner = ActivityDesigner.GetDesigner(currentActivity)
            Dim designer2 As CompositeActivityDesigner = If((Not designer Is Nothing), designer.ParentDesigner, Nothing)
            If (designer2 Is Nothing) Then
                Return Nothing
            End If
            Dim direction As DesignerNavigationDirection = If((TypeOf designer2 Is ParallelActivityDesigner OrElse TypeOf designer2 Is ActivityPreviewDesigner), DesignerNavigationDirection.Right, DesignerNavigationDirection.Down)
            Dim activity As Activity = Nothing
            obj2 = designer2.GetNextSelectableObject(currentActivity, direction)
            Do While (((activity Is Nothing) AndAlso (Not obj2 Is Nothing)) AndAlso (Not obj2 Is currentActivity))
                activity = [TryCast](obj2,Activity)
                obj2 = designer2.GetNextSelectableObject(obj2, direction)
            Loop
            If (activity Is Nothing) Then
                direction = If((TypeOf designer2 Is ParallelActivityDesigner OrElse TypeOf designer2 Is ActivityPreviewDesigner), DesignerNavigationDirection.Left, DesignerNavigationDirection.Up)
                obj2 = designer2.GetNextSelectableObject(currentActivity, direction)
                Do While (((activity Is Nothing) AndAlso (Not obj2 Is Nothing)) AndAlso (Not obj2 Is currentActivity))
                    activity = [TryCast](obj2,Activity)
                    obj2 = designer2.GetNextSelectableObject(obj2, direction)
                Loop
            End If
            If (activity Is Nothing) Then
                activity = designer2.Activity
            End If
            Return activity
        End Function

        Friend Shared Function GetRelativePath(ByVal pathFrom As String, ByVal pathTo As String) As String
            Dim uri As New Uri(pathFrom)
            Dim str As String = Uri.UnescapeDataString(uri.MakeRelativeUri(New Uri(pathTo)).ToString).Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)
            If Not str.Contains(Path.DirectorySeparatorChar.ToString) Then
                str = ("." & Path.DirectorySeparatorChar & str)
            End If
            Return str
        End Function

        Friend Shared Function IsValidImageResource(ByVal designerTheme As DesignerTheme, ByVal directory As String, ByVal path As String) As Boolean
            Dim image As Image = DesignerHelpers.GetImageFromPath(designerTheme, directory, path)
            Dim flag As Boolean = (Not image Is Nothing)
            If (Not image Is Nothing) Then
                image.Dispose
            End If
            Return flag
        End Function

        Friend Shared Sub MakePropertiesReadOnly(ByVal serviceProvider As IServiceProvider, ByVal topComponent As Object)
            Dim hashtable As New Hashtable
            Dim queue As New Queue
            queue.Enqueue(topComponent)
            Do While (queue.Count > 0)
                Dim instance As Object = queue.Dequeue
                If (hashtable.Item(instance.GetHashCode) Is Nothing) Then
                    hashtable.Item(instance.GetHashCode) = instance
                    TypeDescriptor.AddProvider(New ReadonlyTypeDescriptonProvider(TypeDescriptor.GetProvider(instance)), instance)
                    Dim descriptor As PropertyDescriptor
                    For Each descriptor In TypeDescriptor.GetProperties(instance, New Attribute() { BrowsableAttribute.Yes })
                        If Not descriptor.PropertyType.IsPrimitive Then
                            Dim component As Object = descriptor.GetValue(instance)
                            If (Not component Is Nothing) Then
                                Dim converter As TypeConverter = TypeDescriptor.GetConverter(component)
                                Dim context As New TypeDescriptorContext(serviceProvider, descriptor, instance)
                                If converter.GetPropertiesSupported(context) Then
                                    TypeDescriptor.AddProvider(New ReadonlyTypeDescriptonProvider(TypeDescriptor.GetProvider(component)), component)
                                    queue.Enqueue(component)
                                End If
                            End If
                        End If
                    Next
                End If
            Loop
        End Sub

        Private Shared Sub OnExecuteDesignerAction(ByVal sender As Object, ByVal e As EventArgs)
            Dim verb As DesignerVerb = [TryCast](sender,DesignerVerb)
            If (Not verb Is Nothing) Then
                Dim designerAction As DesignerAction = [TryCast](verb.Properties.Item(DesignerUserDataKeys.DesignerAction),DesignerAction)
                If (Not designerAction Is Nothing) Then
                    Dim designer As ActivityDesigner = [TryCast](verb.Properties.Item(DesignerUserDataKeys.Designer),ActivityDesigner)
                    If (Not designer Is Nothing) Then
                        designer.OnExecuteDesignerAction(designerAction)
                    End If
                End If
            End If
        End Sub

        Friend Shared Sub RefreshDesignerActions(ByVal serviceProvider As IServiceProvider)
            If (Not serviceProvider Is Nothing) Then
                Dim service As IDesignerHost = [TryCast](serviceProvider.GetService(GetType(IDesignerHost)),IDesignerHost)
                If (Not service Is Nothing) Then
                    Dim obj2 As Object
                    For Each obj2 In service.Container.Components
                        Dim activity As Activity = [TryCast](obj2,Activity)
                        If (Not activity Is Nothing) Then
                            Dim designer As ActivityDesigner = ActivityDesigner.GetDesigner(activity)
                            If (Not designer Is Nothing) Then
                                designer.RefreshDesignerActions
                            End If
                        End If
                    Next
                End If
            End If
        End Sub

        Friend Shared Sub SerializeDesignerStates(ByVal designerHost As IDesignerHost, ByVal writer As BinaryWriter)
            writer.Write(designerHost.Container.Components.Count)
            Dim component As IComponent
            For Each component In designerHost.Container.Components
                writer.Write(component.Site.Name)
                Dim length As Integer = CInt(writer.BaseStream.Length)
                writer.Write(0)
                Dim designer As ActivityDesigner = [TryCast](designerHost.GetDesigner(component),ActivityDesigner)
                If (Not designer Is Nothing) Then
                    Dim num2 As Integer = CInt(writer.BaseStream.Length)
                    DirectCast(designer, IPersistUIState).SaveViewState(writer)
                    writer.Seek(length, SeekOrigin.Begin)
                    writer.Write(CInt((CInt(writer.BaseStream.Length) - num2)))
                    writer.Seek(0, SeekOrigin.End)
                End If
            Next
        End Sub

        Friend Shared Sub ShowDesignerVerbs(ByVal designer As ActivityDesigner, ByVal location As Point, ByVal designerVerbs As ICollection([Of] DesignerVerb))
            If (Not DesignerHelpers.ShowingMenu AndAlso (designerVerbs.Count <> 0)) Then
                Dim service As IMenuCommandService = [TryCast](designer.Activity.Site.GetService(GetType(IMenuCommandService)),IMenuCommandService)
                If (service Is Nothing) Then
                    Throw New Exception(SR.GetString("General_MissingService", New Object() { GetType(IMenuCommandService).FullName }))
                End If
                Try 
                    Dim verb As DesignerVerb
                    For Each verb In designerVerbs
                        service.AddCommand(verb)
                    Next
                    DesignerHelpers.ShowingMenu = True
                    service.ShowContextMenu(WorkflowMenuCommands.DesignerActionsMenu, (location.X - 2), (location.Y + 1))
                Finally
                    DesignerHelpers.ShowingMenu = False
                    Dim verb2 As DesignerVerb
                    For Each verb2 In designerVerbs
                        service.RemoveCommand(verb2)
                    Next
                End Try
            End If
        End Sub

        Friend Shared Sub ShowError(ByVal serviceProvider As IServiceProvider, ByVal e As Exception)
            If (Not e Is CheckoutException.Canceled) Then
                Do While (TypeOf e Is TargetInvocationException AndAlso (Not e.InnerException Is Nothing))
                    e = e.InnerException
                Loop
                Dim message As String = e.Message
                If ((message Is Nothing) OrElse (message.Length = 0)) Then
                    message = e.ToString
                End If
                DesignerHelpers.ShowMessage(serviceProvider, message, DR.GetString("WorkflowDesignerTitle", New Object(0  - 1) {}), MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
            End If
        End Sub

        Friend Shared Sub ShowError(ByVal serviceProvider As IServiceProvider, ByVal message As String)
            DesignerHelpers.ShowMessage(serviceProvider, message, DR.GetString("WorkflowDesignerTitle", New Object(0  - 1) {}), MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
        End Sub

        Friend Shared Sub ShowHelpFromKeyword(ByVal serviceProvider As IServiceProvider, ByVal helpKeyword As String)
            Dim service As IHelpService = [TryCast](serviceProvider.GetService(GetType(IHelpService)),IHelpService)
            If (Not service Is Nothing) Then
                service.ShowHelpFromKeyword(helpKeyword)
            Else
                DesignerHelpers.ShowError(serviceProvider, DR.GetString("NoHelpAvailable", New Object(0  - 1) {}))
            End If
        End Sub

        Friend Shared Function ShowMessage(ByVal serviceProvider As IServiceProvider, ByVal message As String, ByVal title As String, ByVal buttons As MessageBoxButtons, ByVal icon As MessageBoxIcon, ByVal defaultButton As MessageBoxDefaultButton) As DialogResult
            Dim owner As IWin32Window = Nothing
            Dim service As IUIService = [TryCast](serviceProvider.GetService(GetType(IUIService)),IUIService)
            If (Not service Is Nothing) Then
                owner = service.GetDialogOwnerWindow
            Else
                owner = Form.ActiveForm
            End If
            Dim control As Control = [TryCast](owner,Control)
            Dim options As MessageBoxOptions = 0
            If (owner Is Nothing) Then
                Return MessageBox.Show(message, title, buttons, icon, defaultButton, options)
            End If
            If (Not control Is Nothing) Then
                options = If((control.RightToLeft = RightToLeft.Yes), DirectCast(0, MessageBoxOptions), DirectCast(0, MessageBoxOptions))
            ElseIf (owner.Handle <> IntPtr.Zero) Then
                Dim windowLong As Integer = UnsafeNativeMethods.GetWindowLong(owner.Handle, UnsafeNativeMethods.GWL_EXSTYLE)
                If ((Marshal.GetLastWin32Error = 0) AndAlso ((windowLong And UnsafeNativeMethods.WS_EX_LAYOUTRTL) = UnsafeNativeMethods.WS_EX_LAYOUTRTL)) Then
                    options = 0
                End If
            End If
            Return MessageBox.Show(owner, message, title, buttons, icon, defaultButton, options)
        End Function

        Friend Shared Function SnapToGrid(ByVal location As Point) As Point
            If WorkflowTheme.CurrentTheme.AmbientTheme.ShowGrid Then
                Dim gridSize As Size = WorkflowTheme.CurrentTheme.AmbientTheme.GridSize
                gridSize.Width = (gridSize.Width / 2)
                gridSize.Height = (gridSize.Height / 2)
                location.X = (((location.X / gridSize.Width) * gridSize.Width) + If(((location.X Mod gridSize.Width) > (gridSize.Width / 2)), gridSize.Width, 0))
                location.Y = (((location.Y / gridSize.Height) * gridSize.Height) + If(((location.Y Mod gridSize.Height) > (gridSize.Height / 2)), gridSize.Height, 0))
            End If
            Return location
        End Function

        Friend Shared Sub UpdateSiteName(ByVal activity As Activity, ByVal newID As String)
            If (activity Is Nothing) Then
                Throw New ArgumentException("activity")
            End If
            Dim str As String = newID
            If Helpers.IsActivityLocked(activity) Then
                str = InternalHelpers.GenerateQualifiedNameForLockedActivity(activity, newID)
            End If
            activity.Site.Name = str
            If TypeOf activity Is CompositeActivity Then
                Dim activity2 As Activity
                For Each activity2 In Helpers.GetNestedActivities([TryCast](activity,CompositeActivity))
                    If Helpers.IsActivityLocked(activity2) Then
                        Dim declaringActivity As Activity = Helpers.GetDeclaringActivity(activity2)
                        activity2.Site.Name = (declaringActivity.Site.Name & "." & activity2.Name)
                    End If
                Next
            End If
        End Sub


        ' Properties
        Friend Shared ReadOnly Property DesignerPerUserRegistryKey As String
            Get
                Return (Helpers.PerUserRegistryKey & "\" & DesignerHelpers.WorkflowDesignerSubKey)
            End Get
        End Property


        ' Fields
        Friend Const CodeActivityTypeRef As String = "System.Workflow.Activities.CodeActivity, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        Friend Const DeclarativeRulesRef As String = "System.Workflow.Activities.Rules.RuleConditionReference, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        Friend Const EventHandlersTypeRef As String = "System.Workflow.Activities.EventHandlersActivity, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        Friend Const IfElseBranchTypeRef As String = "System.Workflow.Activities.IfElseBranchActivity, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        Friend Const SequentialWorkflowTypeRef As String = "System.Workflow.Activities.SequentialWorkflowActivity, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        Private Shared ShowingMenu As Boolean = False
        Friend Const StateMachineWorkflowTypeRef As String = "System.Workflow.Activities.StateMachineWorkflowActivity, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        Private Shared ReadOnly WorkflowDesignerSubKey As String = "Workflow Designer"
    End Class
End Namespace

